package com.lianxian.xindong.old.fragment.home.selffile.adapter;

import android.content.Context;
import android.graphics.Matrix;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.lianxian.xindong.old.R;
import com.lianxian.xindong.old.fragment.home.selffile.adapter.utils.VideoPlayAdapter;
import com.lianxian.xindong.old.fragment.home.selffile.entities.VideoData;
import com.lianxian.xindong.old.fragment.home.selffile.view.VideoLoadingProgressbar;

import java.util.List;


public class MainAdapter extends VideoPlayAdapter<MainAdapter.ViewHolder> {
    private Context mContext;
    private int mVideoWidth;
    private int mVideoHeight;
    List<VideoData> datas;
    private int mCurrentPosition;
    private ViewHolder mCurrentHolder;

    private VideoPlayer videoPlayer;
    private TextureView textureView;


    public MainAdapter(Context mContext,List<VideoData> datas) {
        this.mContext = mContext;
        this.datas = datas;
        videoPlayer = new VideoPlayer();
        textureView = new TextureView(mContext);
//        stretching(textureView.getMeasuredWidth(),textureView.getMeasuredHeight());
        videoPlayer.setTextureView(textureView);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(mContext).load(datas.get(position).picUrl).apply(options).into(holder.ivCover);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onPageSelected(int itemPosition, View itemView) {
        mCurrentPosition = itemPosition;
        mCurrentHolder = new ViewHolder(itemView);
        playVideo();
    }

    private void playVideo() {
        videoPlayer.reset();
        mCurrentHolder.pbLoading.setVisibility(View.VISIBLE);
        videoPlayer.setOnStateChangeListener(new VideoPlayer.OnStateChangeListener() {
            @Override
            public void onReset() {
                mCurrentHolder.ivCover.setVisibility(View.VISIBLE);
                mCurrentHolder.pbLoading.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onRenderingStart() {
                mCurrentHolder.ivCover.setVisibility(View.GONE);
                mCurrentHolder.pbLoading.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onProgressUpdate(float per) {
            }

            @Override
            public void onPause() {
                mCurrentHolder.pbLoading.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onStop() {
                mCurrentHolder.ivCover.setVisibility(View.VISIBLE);
                mCurrentHolder.pbLoading.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onComplete() {
                videoPlayer.start();
            }
        });
        if (textureView.getParent() != mCurrentHolder.flVideo) {
            if (textureView.getParent() != null) {
                ((FrameLayout) textureView.getParent()).removeView(textureView);
            }
            mCurrentHolder.flVideo.addView(textureView);

        }

        videoPlayer.setDataSource(datas.get(mCurrentPosition).getVideoUrl());
        videoPlayer.prepare();
    }

    public void release() {
        videoPlayer.release();
    }
    public void pause(){
        videoPlayer.pause();
    }
    public void resume(){
        videoPlayer.start();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private FrameLayout flVideo;
        private ImageView ivCover;
        private VideoLoadingProgressbar pbLoading;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            flVideo = itemView.findViewById(R.id.flVideo);
            ivCover = itemView.findViewById(R.id.ivCover);
            pbLoading = itemView.findViewById(R.id.pbLoading);
        }
    }
    private void stretching(float mtextureViewWidth,float mtextureViewHeight){
        //mtextureViewWidth为textureView宽，mtextureViewHeight为textureView高
        //mtextureViewWidth宽高，为什么需要用传入的，因为全屏显示时宽高不会及时更新
        Matrix matrix = new Matrix();
        //videoView为new MediaPlayer()
        mVideoWidth=videoPlayer.getVideoWidth();
        mVideoHeight=videoPlayer.getVideoHeight();

//        mtextureViewWidth=textureView.getWidth();
//        mtextureViewHeight=textureView.getHeight();

        //得到缩放比，从而获得最佳缩放比
        float sx = mtextureViewWidth / mVideoWidth;
        float sy = mtextureViewHeight / mVideoHeight;
        //先将视频变回原来的大小
        float sx1 =  mVideoWidth / mtextureViewWidth;
        float sy1 = mVideoHeight/mtextureViewHeight;
        matrix.preScale(sx1,sy1);
        Log.d("mat",matrix.toString());
        //然后判断最佳比例，满足一边能够填满
        if(sx>=sy){
            matrix.preScale(sy,sy);
            //然后判断出左右偏移，实现居中，进入到这个判断，证明y轴是填满了的
            float leftX=(mtextureViewWidth - mVideoWidth * sy) / 2;
            matrix.postTranslate(leftX, 0);
        }else{
            matrix.preScale(sx,sx);
            float leftY=(mtextureViewHeight - mVideoHeight*sx) / 2;
            matrix.postTranslate(0, leftY);
        }

        textureView.setTransform(matrix);//将矩阵添加到textureView
        textureView.postInvalidate();//重绘视图

    }
}
